<?php
/*本文件处理把message表中的消息解析后，生成到keyfino表和retrieve表*/
/*目的仅仅是防止每次消息过来要写keyinfo表和retrieve表  @20130925*/
@header('Content-Type: text/html; charset=utf-8');

ProcessMessage();

/*更新retrieve数据表，添加新关键字到keyinfo*/
function ProcessMessage()
{
	$con = mysql_connect("127.0.0.1","root","redhat");
	if (!$con)
	{
		die('Could not connect: ' . mysql_error());
	}

	mysql_select_db("test", $con);
	mysql_query("set names utf8");	
	
	/*先清空*/
	$sqlsentence = "delete from keyinfo where keytype=2";
	$temp = mysql_query($sqlsentence);
	
	$sqlsentence = "update keyinfo set search_times=0";
	$temp = mysql_query($sqlsentence);
	
	$sqlsentence = "delete from retrieve";
	$temp = mysql_query($sqlsentence);
	
	/*开始读取每个message，然后关键字处理。。。*/
	$sqlsentence = "SELECT * FROM message";
	$msg_result = mysql_query($sqlsentence);
	echo "###mysql access: ".$sqlsentence." ###msg_result is ".$msg_result."</br>";
	while($msgRow = mysql_fetch_array($msg_result)){
		$openid = $msgRow['openid'];
		$message = $msgRow['content'];
		$message_time = $msgRow['message_time'];
		
		/*判断哪些是搜索词，符合中文条件。mark to be continued...*/
		/*if(false == ereg('[^x00-xff]+', $message))
		{
			echo "The sentence is not search string!\r\n";
			exit;
		}*/
		if(eregi('[0-9]+', $message)|| eregi('buy', $message)|| eregi('cancel', $message)||(0==strcasecmp('y',$message))||(0==strcasecmp('yes',$message))|| ereg('购买', $message)||ereg('取消', $message))
		{
			continue;
		}
		
		/*先分词*/
		$text = $message;
		$text = iconv("UTF-8", "GBK//IGNORE", $text);
		$text = urlencode($text);
		$result = file_get_contents("http://192.168.1.22:1985/?w=".$text);
		$result = iconv("GBK", "UTF-8//IGNORE", $result);

		echo "Word segment result:".$result."</br>";
		$fenci = explode(" ", $result);
		
		foreach ($fenci as $current_keyword){
			echo "current_keyword:".$current_keyword."</br>";
			
			/*先查找存在否*/
			$sqlsentence = "SELECT * FROM keyinfo where keyword = '".$current_keyword."'";
			$result = mysql_query($sqlsentence);
			echo "###mysql access: ".$sqlsentence." ###result is ".$result."</br>";
			
			/*更新keyinfo中关键字搜索次数。不存在就添加到keyinfo*/
			$numRows = mysql_num_rows($result);
			if($numRows > 0)
			{
				echo "Update search_times!</br>";
				
				$row = mysql_fetch_array($result); /*只取一行*/
				$row['search_times'] += 1;
				
				$sqlsentence = "update keyinfo set search_times=".$row['search_times']." where keyid=".$row['keyid'];
				$result = mysql_query($sqlsentence);
				echo "###mysql access: ".$sqlsentence." ###result is ".$result."</br>";
			}else{
				echo "Create new keyword!</br>";
				
				$sqlsentence = "insert into keyinfo (keytype,keyword,search_times) values (2,'".$current_keyword."',1)";
				$result = mysql_query($sqlsentence);
				echo "###mysql access: ".$sqlsentence." ###result is ".$result."</br>";			
			}
			
			/*更新retrieve数据库*/
			echo "current keyword is ".$current_keyword.". open id is ".$openid."</br>";
			$sqlsentence = "SELECT * FROM keyinfo where keyword = '".$current_keyword."'";
			$result = mysql_query($sqlsentence);
			$keyrow = mysql_fetch_array($result);
			echo "current keyid is ".$keyrow['keyid']."</br>";
			
			$sqlsentence = "insert into retrieve values ('".$openid."',".$keyrow['keyid'].",'".$message_time."')";
			echo $sqlsentence."</br>";
			$result = mysql_query($sqlsentence);
		}
	}
	mysql_close($con);
	return $map_result;
}

?>